/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package ranab.server.ftp.gui;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import ranab.server.ftp.FtpConfig;
import ranab.server.ftp.FtpConnectionObserver;
import ranab.server.ftp.FtpUser;

/**
 * This table model tracks currently logged in users.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>.
 */
public
class FtpConnectionTableModel extends AbstractTableModel 
                              implements FtpConnectionObserver {
    
    private final static SimpleDateFormat DATE_FMT = new SimpleDateFormat("MM/dd HH:mm:ss");
        
    private final static String[] COL_NAMES = {"User", 
                                               "Login Time", 
                                               "Last Access Time",    
                                               "Client"};
    private List mConnectedUserList;
    private FtpConfig mConfig;
    
    /**
     * Constructor - initialize user list
     */
    public FtpConnectionTableModel() {
        mConnectedUserList = new Vector();
    }
    
    /**
     * Reload the model.
     */
    public void refresh(FtpConfig cfg) {
        mConfig = cfg;
        if (mConfig != null) {
            mConnectedUserList = mConfig.getConnectionService().getAllUsers();
            mConfig.getConnectionService().setObserver(this);
        }
        else {
            mConnectedUserList.clear();
        }
        fireTableDataChanged();
    }
     
    /**
     * Get column class - always string
     */
    public Class getColumnClass(int index) {
        return String.class;
    }
    
    /**
     * Get column count.
     */
    public int getColumnCount() {
        return COL_NAMES.length;
    }
    
    /**
     * Get column name.
     */
    public String getColumnName(int index) {
        return COL_NAMES[index];
    } 
    
    /**
     * Get row count.
     */
    public int getRowCount() {
        return mConnectedUserList.size();
    }
    
    /**
     * Is cell editable - currently false.
     */
    public boolean isCellEditable(int row, int col) {
        return true;
    }
    
   /**
    * Set value at - dummy method
    */
   public void setValueAt(Object val, int row, int col) {
   }
   
   /**
    * Find column index.
    */
   public int findColumn(String columnName) {
        int index = -1;
        for(int i=COL_NAMES.length; --i>=0; ) {
            if (COL_NAMES[i].equals(columnName)) {
                index = i;
                break;
            }
        }
        return index;
   }
   
    /**
     * Get value at.
     */
    public Object getValueAt(int row, int col) {
        
        // error check
        String retVal = "";
        
        FtpUser thisUser = null;
        if (row < mConnectedUserList.size()) {
            thisUser = (FtpUser)mConnectedUserList.get(row);
        }
        if (thisUser == null) {
            return retVal;
        }
            
        switch(col) {
            case 0:
                if (thisUser.getName() != null) {
                    retVal = thisUser.getName();
                }
                break;
            
            case 1:
                if (thisUser.getLoginTime() != 0) {
                    retVal = DATE_FMT.format(new Date(thisUser.getLoginTime()));
                }
                break;
            
            case 2:
                if (thisUser.getLastAccessTime() != 0) {
                    retVal = DATE_FMT.format(new Date(thisUser.getLastAccessTime()));
                }
                break;
                
            case 3:
                InetAddress addr = thisUser.getClientAddress();
                if (addr != null) {
                    retVal = addr.getHostAddress();
                }
                break;   
        }
        return retVal;
    }
    
    /**
     * Get user at an index.
     */
    public FtpUser getUser(int index) {
        if(index < mConnectedUserList.size()) {
            return (FtpUser)mConnectedUserList.get(index);
        }
        return null;
    }
     
    ///////////////////////////  Observer Methods ///////////////////////////
    /**
     * Add a new user
     */
    public void newConnection(FtpUser thisUser) {
        mConnectedUserList.add(thisUser);
        int sz = mConnectedUserList.size();
        fireTableRowsInserted(sz, sz);
    }  
    
    /**
     * Close connection.
     */
    public void removeConnection(FtpUser user) {
        int index = mConnectedUserList.indexOf(user);
        if (index != -1) {
            mConnectedUserList.remove(index);
            fireTableRowsDeleted(index, index);
        }        
    } 
    
    /**
     * Existing connected user update notification.
     */
    public void updateConnection(FtpUser user) {
        int index = mConnectedUserList.indexOf(user);
        if(index != -1) {       
            fireTableRowsUpdated(index, index);
        }
    } 

}    
